import json
import os
import tempfile
from fastapi import APIRouter, File, UploadFile, HTTPException, Form
from fastapi.responses import JSONResponse
# from utils.baidu_ocr import BaiduOCR
from qiniu import Auth
from tools.bdapi import bdAPI
from tools.myredis import r
idcard_router = APIRouter(prefix="/idcard", tags=["身份证验证"])

@idcard_router.get("/verify")#客户端调用/idcard/verify获取上传凭证
async def verify_idcard():
    # 需要填写你的 Access Key 和 Secret Key
    access_key = '-YblO8MOdeJTLkQBTL8BZ0mevKdCy04sWwGfVFJJ'
    secret_key = 'PqGsNCT5c__FyRp8Pxe5FyX4sonjevty3BH-esKi'

    # 构建鉴权对象
    q = Auth(access_key, secret_key)

    # 要上传的空间
    bucket_name = '2505'

    # 3600为token过期时间，秒为单位。3600等于一小时
    token = q.upload_token(bucket_name, None, 3600)#获取token
    return token

@idcard_router.get("/redis1")
async def redis_idcard(user_id:str, url:str):
    if not url:
        return JSONResponse(status_code=400, content={"detail": "缺少图片地址"})
    print(url)
    # # e=bdAPI.idcard(url)
    # print(e)
    # return JSONResponse(status_code=200, content={"detail": e})
    #往redis存储图片地址以列表的形式存储
    data = {"user_id":user_id, "url": url}
    r.rpush("idcard", json.dumps(data))
    return JSONResponse(status_code=200, content={"detail": "正在审核中，请等待3天……"})
